package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.AdminPermission;
import com.rongke.model.AdminRolePermission;
import com.rongke.service.AdminPermissionService;
import com.rongke.service.AdminRolePermissionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @AdminPermissionController
 * @Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/adminPermission")
@Transactional
@CrossOrigin
public class AdminPermissionController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private AdminPermissionService adminPermissionService;
    @Autowired
    private AdminRolePermissionService adminRolePermissionService;
    /**
     * @添加
     * @param adminPermission
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addAdminPermission(@RequestBody AdminPermission adminPermission){
        log.debug("添加");
        adminPermissionService.insert(adminPermission);
        return JsonResp.ok(adminPermission);
    }

    /**
     * @修改
     * @param adminPermission
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateAdminPermission(@RequestBody AdminPermission adminPermission){
        log.debug("修改");
        adminPermissionService.updateById(adminPermission);
        return JsonResp.ok(adminPermission);
    }
    /**
     * @根据id查找
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectList", method = RequestMethod.GET)
    public JsonResp selectAdminPermission(String roleId){
        log.debug("获取目录列表");
        EntityWrapper<AdminPermission> ew=new EntityWrapper<>();
        ew.eq("url","#");
        List<AdminPermission> adminPermissions = adminPermissionService.selectList(ew);
        EntityWrapper<AdminRolePermission> ewarp=new EntityWrapper<>();
        ewarp.eq("admin_role_id",roleId);
        List<AdminRolePermission> arpList=adminRolePermissionService.selectList(ewarp);
        for(int  i=0,y=arpList.size();i<y;i++){
            AdminRolePermission arp=arpList.get(i);
            for(int  m=0,n=adminPermissions.size();m<n;m++){
                AdminPermission ap=adminPermissions.get(m);
                if(ap.getId()==arp.getAdminPermissionId()){
                    ap.setChecked(true);
                    break;
                }
            }
        }
        return JsonResp.ok(adminPermissions);
    }

    /**
     * @根据条件查找
     * @param adminPermission
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectAdminPermissionByCondition(@RequestBody AdminPermission adminPermission){
        log.debug("根据条件查找");
        if(adminPermission==null)
            return JsonResp.toFail("参数不能为空");
        List<AdminPermission> adminPermissionList=adminPermissionService.selectByCondition(adminPermission);
        return JsonResp.ok(adminPermissionList);
    }

}
